package com.jqsoft.launcher.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.jqsoft.launcher.common.BphsURL;
import com.jqsoft.launcher.common.Constant;
import com.jqsoft.launcher.dictionarydb.DataDictionaryDao;
import com.jqsoft.launcher.util.WebServiceUtils;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class UpdataService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		new Thread(){
		public void run() {	
			String methodName = BphsURL.getCommonPath("GetDictionaryCatalog_METHOD");
			 String serviceURL = BphsURL.getCommonPath("GetDictionaryCatalog_URL");
//			String methodName = "GetDictionaryCatalog"; 
		//	String serviceURL = "http://192.168.44.138:9998/WebService/DictionaryDetail.asmx";
			String result= WebServiceUtils.requestNoParams(methodName, serviceURL);
//		String result=	WebServiceUtils.requestByParams(methodName, logindata.toString(), serviceURL);
//		{"sSuccess":"1","sMessage":"用户登陆成功！","AllCount":0,"Data":"{\"sResult\":null,\"sMessage\":null,\"sLoginName\":\"admin\",\"sAlias\":null,\"sUserName\":\"安徽省卫生厅\",\"sOrganizationKey\":\"60bfbec0-180e-41b9-9974-007ef0d0ee43\",\"sOrgInstitutionCode\":\"X19870226\",\"sOrganizationName\":\"安徽省卫生厅\",\"sOrganizationTypeCode\":\"10\",\"sOrganizationLevelCode\":\"1\",\"sManagementDivisionCode\":\"340000\",\"sManagementDivisionName\":\"安徽省\"}"}
		if(StringUtils.isNotBlank(result)){
		try {
//			DataDictionaryDao dao=new DataDictionaryDao();
			DataDictionaryDao.deleteAll();
			 JSONArray jsonArray = new JSONArray(result);
			 for (int i = 0; i < jsonArray.length(); ++i) {
	                JSONObject o = (JSONObject) jsonArray.get(i);	
	    			boolean a= DataDictionaryDao.add(o.getString("gKey"), o.getString("sCatalogCode"), o.getInt("iSerialNumber"),  o.getString("sValue"),  o.getString("sCode"), 1);
	            }
//			 updateOver=true;
			 sendGuangBo(Constant.UPDATE_DOWNLOAD_CANCELED);
		} catch (JSONException e) {
//			 updateOver=true;
			sendGuangBo(Constant.UPDATE_DOWNLOAD_ERROR);
			e.printStackTrace();
		}
		}else{
//			 updateOver=true;
			sendGuangBo(Constant.UPDATE_DOWNLOAD_ERROR);
//			handler.sendEmptyMessage(Constant.UPDATE_DOWNLOAD_ERROR);
		}

		}
		}.start();
	}

	public void sendGuangBo(int data){
		 Intent intent = new Intent("com.example.communication.RECEIVER");  
		 intent.putExtra("data", data);  
		 sendBroadcast(intent); 
		
	}
//	private Handler handler = new Handler() {
//		public void handleMessage(android.os.Message msg) {
//			switch (msg.what) {
//			case Constant.UPDATE_DOWNLOAD_CANCELED:
//					to(BphsLoginActivity.class);
//				break;
//			case Constant.UPDATE_DOWNLOAD_ERROR:
//				Toast.makeText(getApplicationContext(), "更新数据库失败！",1).show();
//					to(BphsLoginActivity.class);
//				break;
//			}
//			};
//			};
			
//			public void to( Class<?>  cls) {
//				
//				 Intent intent2=new Intent();                                
//	                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//	                intent2.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//	                intent2.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
//	                intent2.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//	                intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//	                intent2.setClass(getApplicationContext(), cls);
//	                startActivity(intent2);                
//	                onDestroy();
////				Intent intent = new Intent(getApplicationContext(),cls);  
////				startActivity(intent);
////				((Activity) getApplicationContext()).finish();
////				((JQActivity) getApplicationContext()).overridePendingTransition(0, 0);
//			}
}
